package com.lg.wxq.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lg.wxq.bean.Status;
import com.lg.wxq.manager.ImageManager;
import com.lg.wxq.ui.R;
import com.lg.wxq.utils.DebugUtils;
import com.lg.wxq.utils.StringUtils;

public class HomeAdapter extends BaseAdapter {
	private ArrayList<Status> statuses;
	private Context context;

	public HomeAdapter(ArrayList<Status> statuses, Context context) {
		super();
		this.context = context;
		this.statuses = statuses;
	}
	
	public int getCount() {
		return statuses.size();
	}

	public Object getItem(int position) {
		return statuses.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View itemView = null;
		if (convertView != null && convertView.findViewById(R.id.homelistitem_tv_nickname) != null) {
			convertView = null;
		}
		itemView = LayoutInflater.from(context).inflate(R.layout.homelistitem, null);

		Status status = statuses.get(position);

		// 时间
		TextView timeTv = (TextView) itemView.findViewById(R.id.homelistitem_tv_time);
		timeTv.setText(" " + status.getTime() + " ");
		// 昵称
		TextView nicknameTv = (TextView) itemView.findViewById(R.id.homelistitem_tv_nickname);
		nicknameTv.setText(status.getUser().getName());
		// 来自
		TextView souceTv = (TextView) itemView.findViewById(R.id.homelistitem_tv_from);
		souceTv.setText(status.getFrom());
		// 微博
		TextView weiboTv = (TextView) itemView.findViewById(R.id.homelistitem_tv_weibo);
		weiboTv.setText(status.getText());
		// 图片
		LinearLayout imageLl = (LinearLayout) itemView.findViewById(R.id.homelistitem_ll_image);
		if (StringUtils.isNotBlank(status.getThumbnail_pic())) {
			imageLl.setVisibility(View.VISIBLE);
			if (ImageManager.getInstance().getPicture(status.getIdstr()) != null) {
				ImageView imageIv = (ImageView) itemView.findViewById(R.id.homelistitem_iv_image);
				BitmapDrawable picture = ImageManager.getInstance().getPicture(status.getIdstr());
				int intrinsicHeight = picture.getIntrinsicHeight();
				int intrinsicWidth = picture.getIntrinsicWidth();
				Bitmap zoomBitmap = zoomBitmap(picture.getBitmap(), intrinsicWidth * 2, intrinsicHeight * 2);
				imageIv.setImageBitmap(zoomBitmap);
			}
		} else {
			imageLl.setVisibility(View.GONE);
		}
		// 头像
		ImageView avatarIv = (ImageView) itemView.findViewById(R.id.homelistitem_iv_avatar);
		if (ImageManager.getInstance().getAvatar(status.getUser().getIdstr()) != null) {
			avatarIv.setBackgroundDrawable(ImageManager.getInstance().getAvatar(status.getUser().getIdstr()));
		}

		// 转发
		LinearLayout retweetedStatusLl = (LinearLayout) itemView.findViewById(R.id.homelistitem_ll_retweeted_status);
		if (status.getRetweeted_status() != null) {
			retweetedStatusLl.setVisibility(View.VISIBLE);
			TextView transweiboTv = (TextView) itemView.findViewById(R.id.homelistitem_tv_transweibo);
			transweiboTv.setText(status.getRetweeted_status().getUser().getName() + ":" + status.getRetweeted_status().getText());
			LinearLayout transimageLl = (LinearLayout) itemView.findViewById(R.id.homelistitem_ll_transimage);
			if (StringUtils.isNotBlank(status.getRetweeted_status().getThumbnail_pic())) {
				transimageLl.setVisibility(View.VISIBLE);
				if (ImageManager.getInstance().getPicture(status.getRetweeted_status().getIdstr()) != null) {
					ImageView transImageIv = (ImageView) itemView.findViewById(R.id.homelistitem_iv_transimage);
					BitmapDrawable picture = ImageManager.getInstance().getPicture(status.getRetweeted_status().getIdstr());
					int intrinsicHeight = picture.getIntrinsicHeight();
					int intrinsicWidth = picture.getIntrinsicWidth();
					Bitmap zoomBitmap = zoomBitmap(picture.getBitmap(), intrinsicWidth*2,intrinsicHeight*2);
					transImageIv.setImageBitmap(zoomBitmap);
				}
			} else {
				transimageLl.setVisibility(View.GONE);
			}
		} else {
			retweetedStatusLl.setVisibility(View.GONE);
		}

		return itemView;
	}

	public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		Matrix matrix = new Matrix();
		float scaleWidth = ((float) w / width);
		float scaleHeight = ((float) h / height);
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
		return newBmp;
	}
}
